package com.bgs.eat.userAddress.service.impl;

import com.bgs.eat.userAddress.entity.UserAddressEntity;
import com.bgs.eat.userAddress.mapper.UserAddressMapper;
import com.bgs.eat.config.CommonResult;
import com.bgs.eat.userAddress.service.UserAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName AddressServiceImpl
 * @Description TODO
 * @Author hbx
 * @Date 2022/10/25 13:58
 **/
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    UserAddressMapper userAddressMapper;


    /**
    * @Author hbx
    * @Description 新增收货地址
    * @Date 2022/10/25 14:12
    * @param addressEntity
    */
    @Override
    public Integer addAddress(UserAddressEntity addressEntity) {
        addressEntity.setDelFlag(0);
        addressEntity.setAddressType(1);
        int i = userAddressMapper.insertSelective(addressEntity);
        return i;
    }

    /**
    * @Author hbx
    * @Description 根据用户id查询其他地址信息
    * @Date 2022/10/25 14:57
    * @param userProtalId
    */
    @Override
    public List<UserAddressEntity> getAllAddressByUserId(Integer userProtalId) {
        Example example = new Example(UserAddressEntity.class);
        example.createCriteria().andEqualTo("delFlag",0).andEqualTo("userProtalId",userProtalId);
        return userAddressMapper.selectByExample(example);
    }

    /**
    * @Author hbx
    * @Description 根据id查询地址信息
    * @Date 2022/10/25 16:37
    * @param id
    */
    @Override
    public CommonResult getAllAdderssById(Integer id) {
        Example example = new Example(UserAddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserAddressEntity addressEntity = userAddressMapper.selectOneByExample(example);
        return CommonResult.ok(addressEntity);
    }

    /**
    * @Author hbx
    * @Description 修改用户地址信息
    * @Date 2022/10/26 9:53
    * @param addressEntity
    */
    @Override
    public Integer updateAdderss(UserAddressEntity addressEntity) {
        return userAddressMapper.updateByPrimaryKeySelective(addressEntity);
    }

    /**
    * @Author hbx
    * @Description 根据id删除用户地址信息
    * @Date 2022/10/26 13:55
    * @param id
    */
    @Override
    public Integer deleteAddressById(Integer id) {
        Example example = new Example(UserAddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserAddressEntity userAddress = new UserAddressEntity();
        UserAddressEntity userAddress1 = userAddressMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userAddress1,userAddress);
        userAddress.setDelFlag(1);
        return userAddressMapper.updateByExample(userAddress,example);
    }

    /**
    * @Description 获取用户常用地址信息列表（门户用户版）
    * @Author lzy
    * @Date 2022/10/28 14:09
    * @Return com.bgs.eat.config.CommonResult
    * @param userId
    */
    @Override
    public CommonResult getUserProtalAddress(Integer userId) {
        UserAddressEntity userAddressEntity = new UserAddressEntity();
        userAddressEntity.setUserProtalId(userId);
        List<UserAddressEntity> userAddressEntities = userAddressMapper.select(userAddressEntity);
        return CommonResult.ok(userAddressEntities);
    }

    /**
    * @Author hbx
    * @Description 修改地址类型根据id
    * @Date 2022/10/28 9:57
    * @param id
    */
    @Override
    public Integer updateFirstAdderssType(Integer id) {
        Example example = new Example(UserAddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserAddressEntity userAddress = new UserAddressEntity();
        UserAddressEntity userAddressEntity = userAddressMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userAddressEntity,userAddress);
        userAddress.setAddressType(0);
        return userAddressMapper.updateByExample(userAddress,example);
    }
}
